package edu.uit.se.laps.web.controller;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;

import edu.uit.se.laps.web.model.SanPham;
import edu.uit.se.laps.web.model.TaiKhoan;
import edu.uit.se.laps.web.util.HibernateUtil;

public class SanPhamManager extends HibernateUtil{
	
	public SanPham addSanPham(SanPham sanpham) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(sanpham);
        session.getTransaction().commit();
        return sanpham;
    }
	
    public SanPham deleteSanPham(int id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        //SanPham sanpham = (SanPham) session.load(SanPham.class, id);
        SanPham sanpham = (SanPham) session.get(SanPham.class, id);
        if(null != sanpham) {
            session.delete(sanpham);
        }
        session.getTransaction().commit();
        return sanpham;
    }
 
    public List<SanPham> list() {
         
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<SanPham> sanpham = null;
        try {
             
            sanpham = (List<SanPham>)session.createQuery("from SanPham").list();
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return sanpham;
    }
    
    public boolean updateSanPham(SanPham sanpham){
		boolean result = false;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.update(sanpham);
			result = true;
		} catch (Exception e) {
			session.getTransaction().rollback();
		}		
		session.getTransaction().commit();
		return result;
	}
    
    public SanPham getSanPham(int id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        
        
        SanPham sanpham = (SanPham) session.get(SanPham.class, id);
        if (null != sanpham) {
        	session.getTransaction().commit();
            return sanpham;
		} else {
			session.getTransaction().rollback();
	        return null;

		}
        
    }
    
    
     
	public List<SanPham> listSp(int loaiSp) {
	        
	        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
	        List<SanPham> sanpham = null;
	        try {
	        	String sql = "from SanPham where idLoaiSP = ?";
				Query query = session.createQuery(sql);
				query.setInteger(0, loaiSp);
				
	            sanpham = (List<SanPham>)query.list();        
	            				
	        } catch (HibernateException e) {
	            e.printStackTrace();
	            session.getTransaction().rollback();
	        }
	        session.getTransaction().commit();
	        return sanpham;
	    }
    
    
}
